﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TSites.Xml;
using System.IO;

namespace TSites.Config
{
    /// <summary>
    /// Use to contain config info;
    /// </summary>
    public class TsConfig
    {
        #region Member variables

        private static TsConfig _instance;
        private static TsXmlEngine _xmlEngine;
        private static String _configFile;
        #endregion

        #region Properties

        public string LanguagePath { get; set; }
        public string MessagePath { get; set; }
        public string ParamPattern { get; set; }
        public string MasterCodeFile { get; set; }
        public string DefaultLanguage { get; set; }

        #endregion

        #region Constructor

        /// <summary>
        /// Constructor
        /// </summary>
        private TsConfig() { }

        #endregion

        #region Function

        /// <summary>
        /// Get instance of TsConfig
        /// </summary>
        /// <returns>TsConfig</returns>
        public static TsConfig GetInstance()
        {
            if (_instance == null)
                _instance = new TsConfig();            
            string configFile = HttpContext.Current.Server.MapPath(@"~/Content/config/config/configFile.txt");
            _instance.LoadFile(configFile);
            return _instance;
        }

        public void LoadFile(string configFile)
        {
            if (_xmlEngine == null)
            {
                _configFile = configFile;
                _xmlEngine = new TsXmlEngine(configFile);
            }
            else if (!configFile.Equals(configFile))
            {
                _configFile = configFile;
                _xmlEngine = new TsXmlEngine(configFile);
            }

            // Read configuaration file.
            MasterCodeFile = HttpContext.Current.Server.MapPath(_xmlEngine.GetContent("type", "masterCodeFile"));
            LanguagePath = HttpContext.Current.Server.MapPath(_xmlEngine.GetContent("type", "languagePath"));
            MessagePath = HttpContext.Current.Server.MapPath(_xmlEngine.GetContent("type", "messagePath"));
            ParamPattern = _xmlEngine.GetContent("type", "paramPattern");
            DefaultLanguage = _xmlEngine.GetContent("type", "defaultLanguage");
        }

        #endregion
    }
}